#include "EventPool.h"
#include "TcpConnect.h"
#include "Acceptor.h"
#include <iostream>
#include <utility>
#include <functional>
#include <unistd.h>

using std::cout;
using std::endl;
using std::function;

using TcpConnectPtr = std::shared_ptr<TcpConnect>;
using callback = std::function<void(TcpConnectPtr)>;

void onConnect(TcpConnectPtr ptr)
{
    cout << ptr->toString() << " handle connect!" << endl;
}

void onHandleMessage(TcpConnectPtr ptr)
{
    // cout << "use count = " << ptr.use_count() << endl;
    cout << ">> msg from client:" << ptr->receive();
    ptr->send("hello worlds...\n");
}

void onClosed(TcpConnectPtr ptr)
{
    cout << ptr->toString() << " disconnect!" << endl;
    ptr->shutdown();
}

void test()
{
    Acceptor acc("127.0.0.1", 12345);
    acc.init();

    EventPool loop(acc);

    loop.setConnectCallback(std::move(onConnect));
    loop.setMessageCallback(std::move(onHandleMessage));
    loop.setClosedCallback(std::move(onClosed));

    loop.loop();
}

int main(int argc, char const *argv[])
{
    test();
    return 0;
}
